package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.IEmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

/**
 * <b><code>EmployeeServiceImpl</code></b>
 * <p/>
 * 员工信息service实现类
 * <p/>
 * <b>Creation Time:</b> 16/06/2024 20:44.
 *
 * @author yanjr
 * @since reggie_take_out
 */
@Service("employeeService")
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    /**
     * 员工登录
     * @param employee 用户名和密码
     * @return 用户信息
     */
    @Override
    public Employee login(Employee employee) {
        // 密码md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = this.getOne(queryWrapper);
        // 查到的话，比对密码，查看用户是否禁用
        if (emp != null) {
            if (password.equals(emp.getPassword())) {
                return emp;
            }
        }
        return null;
    }

    /**
     * 新增员工
     * @param employee 员工信息
     * @return 是否添加成功
     */
    @Override
    public R<String> addEmployee(Employee employee) {
        // 判断账号是否已经存在
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>().eq(Employee::getUsername, employee.getUsername());
        Employee emp = this.getOne(queryWrapper);
        if (emp != null) {
            return R.error("账号已存在，请重新输入");
        }
        // 设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        boolean flag = this.save(employee);
        if (flag) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    /**
     * 用户分页查询
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param name 用户名
     */
    @Override
    public R<Page<Employee>> queryEmployee(int page, int pageSize, String name) {
        // 分页信息
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        // 查询条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<Employee>().like(StrUtil.isNotEmpty(name), Employee::getName, name).orderByDesc(Employee::getUpdateTime);
        Page<Employee> result = baseMapper.selectPage(pageInfo, queryWrapper);
        return R.success(result);
    }

    /**
     * 更新 用户
     * @param employee 请求参数
     * @return 是否成功
     */
    @Override
    public R<String> updateEmployee(Employee employee) {
        boolean flag = this.updateById(employee);
        if (flag) {
            return R.success("OK");
        }
        return R.error("fail");
    }

    /**
     * 根据用户id查询用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @Override
    public R<Employee> queryEmployeeById(Long id) {
        Employee employee = this.getById(id);
        if (employee == null) {
            return R.error("没有查到对应用户");
        }
        return R.success(employee);
    }
}
